package com.hwadee.hello.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.hwadee.hello.entity.Classlist;
import com.hwadee.hello.entity.Student;
import com.hwadee.hello.service.StudentService;

@Controller
//@RequestMapping("/u")
public class StudentsController {
	
	@Autowired
	private StudentService studentService;
	
	//@RequestMapping(value = "/students", method = RequestMethod.GET)
	@GetMapping("/students")
	public ResponseEntity<PageInfo<Student>> get(@RequestParam(value = "p", defaultValue = "1") int p,String wd) {
		PageInfo<Student> pi = studentService.getStudents(p);
		//model.addAttribute("pi", pi);
		//WEB-INF/views/student/list.jsp
		return new ResponseEntity<PageInfo<Student>>(pi, HttpStatus.OK);
	}
	
	//@RequestMapping(value = "/delete/{id}", params = {"param1=value1", "!param2"})
	//delete/{id} @PathVariable("id")
	//delete?id=5 @RequestParam("id") 
	@GetMapping("/delete/{id}")
	public String delete(@PathVariable("id") int id) {
		int rows = studentService.removeStudent(id);
		return "redirect:/students";
	}
	@GetMapping("/update/{id}")
	public String updating(@PathVariable("id") int id, Model model) {
		Student stu = studentService.getStudentById(id);
		model.addAttribute("stu", stu);
		return "student/update";
	}
	//2000-10-10
	@PostMapping("/update")
	public String update(Student stu) {
		System.out.println( stu );
		
		return "redirect:/students";
	}
	@GetMapping("/date")
	public @ResponseBody Date date(@DateTimeFormat(pattern="yyyy-MM-dd") Date birth) {
		
		return birth;
	}
	@PostMapping("/array")
	public @ResponseBody String array(String [] hobbies) {
		
		return Arrays.toString(hobbies);
	}
	// list: students[0].stuName
	// map:  students['key'].stuName
	@PostMapping("/list")
	public @ResponseBody List<Student> list(Classlist list){
		
		return list.getStudents();
	}

	/*
	 * //json:{ "stuId": 999, "stuName": "马2六", "stuSex": "M", "stuAge": 19,
	 * "stuPro": "会计", "stuGrade": null, "stuIntegrity": 3, "classlist": null }
	 */ 
	/*
	 * xml:<?xml version="1.0"
	 * encoding="UTF-8"?><Student><stuId>999</stuId><stuName>马2六</stuName><stuSex>M<
	 * /stuSex><stuAge>19</stuAge><stuPro>会计</stuPro><stuGrade/><stuIntegrity>3</
	 * stuIntegrity><classlist/></Student>
	 */
	@PostMapping("/json")
	public @ResponseBody Student json(@RequestBody Student stu) {
		return stu;
	}

}
